/**************************************************************************************
* Item.java
* 
* Item mechanics:
* ITEM TYPES
Weapon - used to attack1 in melee. Provides attack1 power. Requires Brawn to use and has 2 use thresholds (one for wielding with both hands, and a higher one for wielding with one hand).
Firearm - used to shoot bullets. Provides range and can modify damage (bullets provide base attack1 power). Requires grace to use and has two thresholds: one to shoot with recoil (unable to shoot again next turn) and a higher one to avoid recoil.
Armor - provides defense. Requires brawn to be effective in; for each point of shortfall you suffer a -10% penalty to defense, using magic, and all skills. Also if your brawn is insufficient you will be encumbered - automatically fail jump and swim tests.
Shield - a wielded item that provides defense and requires brawn to use. Has two requirements, the lower to use without a penalty (-20% defense per shortfall) and a higher requirement to be able to block projectiles (20% + 5% per point of surplus brawn). Surplus brawn adds 10% to defense of shield value (min 1 per surplus), limited to the original shield defense.
Talisman - a wielded item that provides magic bonuses and requires will to use, with insufficient will effects are reduced by 20%, surplus will adds 10%.
IN GENERAL, stat shortfall has a 20% penalty per point, while stat surplus has a 10% bonus per point but is capped by the original value of the item.

Critical Hits:
Swords - cause wounds on critical
Maces - cause stunning on critical
Axes - extra damage
Spears - cause impalement (stuck+free attack1 if you move away from the creature - or maybe drag it?) on critical

FIREARMS
Pistols: short range, usually weak ammo types, low DEX requirements per damage. High capacity. The Machine Pistol has a high ROF.
Rifles: long range, usually strong ammo types, high DEX requirements. Low capacity for strongest ammo, weaker ammo for high-capacity/high ROF types.
Carbines: Good capacity, medium range, usually weak ammo types
Shotguns: AOE ammo types, short range, low capacity. Low DEX requirement.
Machine Guns: high ROF, strong (rifle) ammo types, very high ammo use.
Heavy Guns: Usually strong ammo types with AOE or other special effect (flamer has a beam effect), very heavy, low capacity usually, ammo is rare or expensive
Ray Guns: low DEX requirements, uses charges rather than ammo, varied effects, requires a Tech skill check to use.

*************************************************************************************/
package creid.mythos.engine;

import java.util.LinkedList;

public class Item extends Entity
{
	//Constants/////////////////////////////////////////////////////////////////////////
	//Item types
	public static final int I_NATURAL_ATTACK = 0; //an intrinsic weapon
	public static final int I_NATURAL_ARMOR = 1; //an intrinsic armor
	public static final int I_MISC = 2; //misc item
	public static final int I_TRINKET = 3; //misc item
	public static final int I_WEAPON = 4; //misc item
	public static final int I_OUTFIT = 5; //misc item
	public static final int I_HEADGEAR = 6; //misc item
	public static final int I_GUN = 7; //misc item
	public static final int I_POTION = 8; //misc item
	public static final int I_SCROLL = 9; //misc item
	public static final int I_EFFECT = 9; //misc item
	
	private static final long serialVersionUID = -3821320790608078520L;

	//Attributes////////////////////////////////////////////////////////////////////////
	
	//Attack or defense power
	private int power;
	
	//Attack element
	private int element;
	
	//Type of Item
	private int type;
	
	//Use verb
	private String useVerb;
	
	private Item use;

	//Constructors//////////////////////////////////////////////////////////////////////

	public Item(int id, String singular, String plural, char[] sprite, int level, int rarity, int type, int power, int element, String useVerb)
	{
		super(id, singular, plural, sprite, level, rarity);
		this.power = power;
		this.type = type;
		this.useVerb = useVerb;
		setElement(element);
		use = null;
	}
	
	public Item(Item other)
	{
		super(other);
		power = other.power;
		type = other.type;
		useVerb = other.useVerb;
		setElement(other.getElement());
		if (other.use == null)
			use = null;
		else
		{
			use = new Item(other.getUse());
		}
	}
	
	//Methods///////////////////////////////////////////////////////////////////////////

	public int getPower()
	{
		int adjPower = power;
		adjPower -= getProperty("damage");
		
		if (type == I_WEAPON)
			adjPower += getProperty("sharpness");
		else if (type == I_GUN)
			adjPower += getProperty("firepower");
		
		return adjPower;
	}
	
	public int getMass()
	{
		//mass is based on base power
		int adjMass = power;
		
		adjMass += getProperty("heavy");
		adjMass -= getProperty("light");
		
		return adjMass;
	}

	public Item getUse()
	{
		return use;
	}

	public void setUse(Item use)
	{
		this.use = use;
	}
	
	public void setPower(int power)
	{
		this.power = power;
	}

	public String getUseVerb()
	{
		return useVerb;
	}

	public void setUseVerb(String useVerb)
	{
		this.useVerb = useVerb;
	}

	public int getElement()
	{
		return element;
	}

	public void setElement(int element)
	{
		this.element = element;
	}

	public int getType()
	{
		return type;
	}

	public void setType(int type)
	{
		this.type = type;
	}
	
	private String getPowerString()
	{
		int p = getPower();
		int m = getMass();
		
		if (p == m)
			return "" + p;
		else
			return p + "/" + m;
	}
	
	@Override
	public String getName(boolean article)
	{
		switch(type)
		{
		case I_WEAPON:
			return super.getName(article) + " (" + getPowerString() + ")";
		case I_OUTFIT:
		case I_HEADGEAR:
			return super.getName(article) + " [" + getPowerString() + "]";
		case I_GUN:
			return super.getName(article) + " {" + getPowerString() + ", " + getProperty("loaded") + "/" + getProperty("capacity") + "}";
		default:
			return super.getName(article);
		}
	}
	
	@Override
	public String[] getDetails()
	{
		LinkedList<String> details = new LinkedList<String>();
		details.add(getName(Entity.INDEFINITE));
		
		if (super.getDetails() != null)
			for(String s: super.getDetails())
				details.add(s);
			
		return details.toArray(new String[details.size()]);
	}
	
	public int getShotDamage()
	{
		return getProperty("firepower");
	}
	
	public int getThrowDamage()
	{
		int damage = 1;
		
		if (hasProperty("throw"))
		{
			damage = getProperty("throw");
		}
		
		return damage;
	}
	
	public String getThrowName()
	{
		return super.getName(INDEFINITE) + " {" + getThrowDamage() + "}";
	}
	
	public boolean isConsumable()
	{
		boolean consume = false;
		
		switch(type)
		{
		case I_POTION:
		case I_SCROLL:
			consume = true;
		}
		
		return consume;
	}
}
